<?php

namespace Home\Controller;

class ArticleController extends CommonController
{
    protected $page_as = 'article';

    /** 文章列表 */
    public function lists()
    {
        $this->display();
    }

    /** 文章详情 */
    public function view()
    {
        $this->display();
    }

    /** 分享文章页 */
    public function add()
    {
        $SMSopen = C('YTATA_SMS_OPEN');
        $uid     = session('ytata.uid');

        if (empty($SMSopen) && empty($uid)) {
            $this->error('非法请求，短信未配置，不允许快捷发布');
        }

        $oauth_list = ytata_get_cache('oauth');

        $this->assign(compact('oauth_list'));

        $this->display();
    }

    /** 保存文章 */
    public function addsave()
    {
        if (IS_POST) {
            // 用户登录
            if (!D('User')->is_login()) {
                $this->check_smscode_login();  // 短信验证码快捷登录
            }

            $uid  = session('ytata.uid');
            $data = I('post.');

            $ret = D('Article')->article_add($data, $uid);
            if ($ret['status'] === false) {
                $this->error($ret['content']);
            }

            $this->redirect('Article/view', array('id' => $ret['aid']));
        } else {
            $this->error('非法请求');
        }
    }

    /** 点赞 */
    public function ajax_inc_praise()
    {
        if (IS_AJAX) {
            $aid = I('request.aid', '', 'intval');
            $uid = session('ytata.uid');
            if (empty($uid)) {
                $ajax['status']  = 0;
                $ajax['content'] = '请登录后操作！';
                $this->ajaxReturn($ajax);
            }

            $ret = D('ArticlePraise')->article_praise($aid, $uid);
            if ($ret['status'] === false) {
                $ajax['status']  = 0;
                $ajax['content'] = $ret['content'];
                $this->ajaxReturn($ajax);
            } else {
                $ajax['status']  = 1;
                $ajax['content'] = $ret['count'];
                $this->ajaxReturn($ajax);
            }
        }
    }
}